package com.yjlove.myplay.ui.activity;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.Toast;
import com.yjlove.myplay.R;
import com.yjlove.myplay.base.BaseActivity;
import com.yjlove.myplay.common.StringSpKey;
import com.yjlove.myplay.utils.SP;

/**
 * 作者 YJlvoe
 * 创建时间 2017/1/9.
 */
public class TimerTestActivity extends BaseActivity implements View.OnClickListener {

    private Button bt1, bt2, bt3;
    private ProgressBar pb1, pb2, pb3;
    private MyCount mc1,mc2, mc3;
    private boolean mc1Click = false;
    private boolean mc2Click = false;
    private boolean mc3Click = false;
    private AutoCompleteTextView autoText;
    private Button saveText;

    @Override
    protected String getActivityTitle() {
        return getIntent().getStringExtra("title");
    }

    @Override
    protected int getActivityView() {
        return R.layout.activity_timertest;
    }

    @Override
    protected void initView() {

        bt1 = ((Button) findViewById(R.id.bt1));
        bt2 = ((Button) findViewById(R.id.bt2));
        bt3 = ((Button) findViewById(R.id.bt3));

        pb1 = ((ProgressBar) findViewById(R.id.pb1));
        pb2 = ((ProgressBar) findViewById(R.id.pb2));
        pb3 = ((ProgressBar) findViewById(R.id.pb3));

        autoText = (AutoCompleteTextView) findViewById(R.id.auto_text);
        saveText = (Button) findViewById(R.id.save_text);
    }

    @Override
    protected void initData() {
        bt1.setOnClickListener(this);
        bt2.setOnClickListener(this);
        bt3.setOnClickListener(this);

        mc1 = new MyCount(30000, 1000);
        mc1.setPb(bt1, pb1);
        mc2 = new MyCount(30000, 1000);
        mc2.setPb(bt2, pb2);
        mc3 = new MyCount(30000, 1000);
        mc3.setPb(bt3, pb3);

        initHistory();
        initEvent();
    }
    /**
     * 初始化历史记录，最多显示3项提示，采用sp保存
     */
    int nearHistory = 3;//显示记录条数
    private void initHistory() {
//        SharedPreferences sp = getSharedPreferences("auto_text", MODE_PRIVATE);
//        String history = sp.getString("history", "");
        String history = SP.get(StringSpKey.AUTO_TEXT, "");
        String[] hisArrays = history.split(",");
        ArrayAdapter<String> hisAdapter = new ArrayAdapter<>(this, android.R.layout.simple_dropdown_item_1line, hisArrays);
        //TODO:这个字符串会越来越长,开发中最好提供清理历史记录的功能
        if (hisArrays.length > nearHistory) {
            String[] newArrays = new String[nearHistory];
            System.arraycopy(hisArrays, 0, newArrays, 0, nearHistory);
            //布局可以自己创建,用android自带的也行
            hisAdapter = new ArrayAdapter<>(this, android.R.layout.simple_dropdown_item_1line, newArrays);
        }
        autoText.setAdapter(hisAdapter);//这里要传递的adapter参数必须是继承ListAdapter和Filterable的
//        mAutoTv.setDropDownHeight(350);//设置提示下拉框的高度，注意，这只是限制了提示下拉框的高度，提示数据集的个数并没有变化
        autoText.setThreshold(1);//当输入一个字符时就开始搜索历史,默认是2个
        autoText.setCompletionHint("最近的3条记录");

    }

    private void initEvent() {
        //有焦点时才会显示记录
        autoText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                AutoCompleteTextView view = (AutoCompleteTextView) v;
                if (hasFocus) {
                    String history = SP.get(StringSpKey.AUTO_TEXT, "");
//                    SharedPreferences sp = getSharedPreferences("auto_text", MODE_PRIVATE);
//                    String history = sp.getString("history", "");
                    //history不为空才显示
                    if (!TextUtils.isEmpty(history))
                        view.showDropDown();
                }
            }
        });
        saveText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                saveHistory(autoText);
            }
        });
    }

    /**
     * 把内容保存到sp中
     */
    private void saveHistory(AutoCompleteTextView autoTv) {
        String text = autoTv.getText().toString();
        String history = SP.get(StringSpKey.AUTO_TEXT, "");
//        SharedPreferences sp = getSharedPreferences("auto_text", MODE_PRIVATE);
//        String history = sp.getString("history", "");
        //先判断是否已经存过一样的文字
        if (!TextUtils.isEmpty(text)&&!history.contains(text + ",")) {
            StringBuilder sb = new StringBuilder(history);
            sb.insert(0, text + ",");//插入到第一个,代表最新
            //sp.edit().putString("history", sb.toString()).apply();
            SP.save(StringSpKey.AUTO_TEXT, sb.toString());
            Toast.makeText(TimerTestActivity.this, "存好了", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(TimerTestActivity.this, "存过了", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt1:
                if (mc1Click) {

                    mc1.cancel();
                    pb1.setVisibility(View.GONE);
                    mc1Click = false;
                } else {

                    pb1.setVisibility(View.VISIBLE);
                    mc1.start();
                    mc1Click = true;
                }
                break;
            case R.id.bt2:

                if (mc2Click) {
                    pb2.setVisibility(View.GONE);
                    mc2.cancel();
                    mc2Click = false;
                } else {
                    pb2.setVisibility(View.VISIBLE);
                    mc2.start();
                    mc2Click = true;
                }
                break;
            case R.id.bt3:

                if (mc3Click) {
                    pb3.setVisibility(View.GONE);
                    mc3.cancel();
                    mc3Click = false;
                } else {
                    pb3.setVisibility(View.VISIBLE);
                    mc3.start();
                    mc3Click = true;
                }
                break;
        }
    }

    /*定义一个倒计时的内部类*/
    class MyCount extends CountDownTimer {
        Button mBt;
        ProgressBar mPb;
        public MyCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }
        public void setPb(Button bt, ProgressBar pb) {
            mBt = bt;
            mPb = pb;
        }
        @Override
        public void onFinish() {
            mPb.setVisibility(View.GONE);
        }
        @Override
        public void onTick(long millisUntilFinished) {
            mBt.setText("请等待30秒(" + millisUntilFinished / 1000 + ")...");
            //Toast.makeText(TimerTestActivity.this, millisUntilFinished / 1000 + "", Toast.LENGTH_LONG).show();
        }
    }
}
